<?php

// YouTube Downloader PHP
// based on youtube-dl in Python http://rg3.github.com/youtube-dl/
// by Ricardo Garcia Gonzalez and others (details at url above)
//
// Takes a VideoID and outputs a list of formats in which the video can be
// downloaded
//header("Access-Control-Allow-Origin: *");
require_once ("../library/curl.php");
 
if (isset($_REQUEST['videoid']))
{
    $my_id = $_REQUEST['videoid'];
    if (strlen($my_id) > 11)
    {
        $url = parse_url($my_id);
        $my_id = NULL;
        if (is_array($url) && count($url) > 0 && isset($url['query']) && !empty($url['query']))
        {
            $parts = explode('&', $url['query']);
            if (is_array($parts) && count($parts) > 0)
            {
                foreach ($parts as $p)
                {
                    $pattern = '/^v\=/';
                    if (preg_match($pattern, $p))
                    {
                        $my_id = preg_replace($pattern, '', $p);
                        break;
                    }
                }
            }
            if (!$my_id)
            {
                echo '<p>No video id passed in</p>';
                exit;
            }
        }
        else
        {
            echo '<p>Invalid url</p>';
            exit;
        }
    }
}
else
{
    echo '<p>No video id passed in</p>';
    exit;
}

if (isset($_REQUEST['type']))
{
    $my_type = $_REQUEST['type'];
}
else
{
    $my_type = 'redirect';
}

if (isset($_REQUEST['debug']))
{
    $debug = TRUE;
}
else
{
    $debug = FALSE;
}

if ($my_type == 'Download')
{
?>
<div class="download">
<?php

} // end of if for type=Download

    /* First get the video info page for this video id */
    $my_video_info = 'http://www.youtube.com/get_video_info?&video_id=' . $my_id;

    $my_video_info = curlGet($my_video_info);

    /* TODO: Check return from curl for status code */

    $thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';

    parse_str($my_video_info);
//echo '<p><img src="'. $thumbnail_url .'" border="0" hspace="2" vspace="2"></p>';
    $my_title = $title;

    if (isset($url_encoded_fmt_stream_map))
    {
        /* Now get the url_encoded_fmt_stream_map, and explode on comma */
        $my_formats_array = explode(',', $url_encoded_fmt_stream_map);
        //if($debug) {
        //echo '<pre>';
        //print_r(count($my_formats_array));
        //echo '</pre>';
        //}
    }
    else
    {
        echo $my_video_info;
    }
    if (count($my_formats_array) == 1)
    {
        echo '<p>Can not make download Link.</p>';
        exit;
    }

    /* create an array of available download formats */
    $avail_formats[] = '';
    $i = 0;
    $ipbits = $ip = $itag = $sig = $quality = '';
    $expire = time();

    foreach ($my_formats_array as $format)
    {
        parse_str($format);
        $avail_formats[$i]['itag'] = $itag;
        $avail_formats[$i]['quality'] = $quality;
        $type = explode(';', $type);
        $avail_formats[$i]['type'] = $type[0];
        $avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
        parse_str(urldecode($url));
        $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
        $avail_formats[$i]['ipbits'] = $ipbits;
        $avail_formats[$i]['ip'] = $ip;
        $i++;
    }

    if ($debug)
    {
        echo '<p>These links will expire at ' . $avail_formats[0]['expires'] . '</p>';
        echo '<p>The server was at IP address ' . $avail_formats[0]['ip'] . ' which is an ' . $avail_formats[0]['ipbits'] . ' bit IP address. ';
        echo 'Note that when 8 bit IP addresses are used, the download links may fail.</p>';
    }
    if ($my_type == 'Download')
    {


        if (count($avail_formats) > 0)
        {
            echo "<div class='dl_list'>";
            /* now that we have the array, print the options */
            for ($i = 0; $i < count($avail_formats); $i++)
            {
                echo '<p>' .
                '<!--<span class="itag">' . $avail_formats[$i]['itag'] . '</span>--> ' .
                '<!--<a data-ajax="false" href="' . $avail_formats[$i]['url'] . '" class="mime">-->' . $avail_formats[$i]['type'] . '<!--</a>--> ' .
                '<small>(' . $avail_formats[$i]['quality'] . ' / ' .
                '<a data-ajax="false" href="/download.php?mime=' . $avail_formats[$i]['type'] . '&title=' . urlencode($my_title) . '&token=' . base64_encode($avail_formats[$i]['url']) . '" class="dl">download</a>' .
                ')</small></p>';
            }
            echo '</div>';
        }
        ?>



        <?php

    }
    else
    {

        /* In this else, the request didn't come from a form but from something else
         * like an RSS feed.
         * As a result, we just want to return the best format, which depends on what
         * the user provided in the url.
         * If they provided "format=best" we just use the largest.
         * If they provided "format=free" we provide the best non-flash version
         * If they provided "format=ipad" we pull the best MP4 version
         *
         * Thanks to the python based youtube-dl for info on the formats
         *   							http://rg3.github.com/youtube-dl/
         */

//$format =  free['format'];
        $format = "free";
        $target_formats = '';
        switch ($format)
        {
            case "best":
                /* largest formats first */
                $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
                break;
            case "free":
                /* Here we include WebM but prefer it over FLV */
                $target_formats = array('38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13');
                break;
            case "ipad":
                /* here we leave out WebM video and FLV - looking for MP4 */
                $target_formats = array('37', '22', '18', '17');
                break;
            default:
                /* If they passed in a number use it */
                if (is_numeric($format))
                {
                    $target_formats[] = $format;
                }
                else
                {
                    $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
                }
                break;
        }

        /* Now we need to find our best format in the list of available formats */
        $best_format = '';
        for ($i = 0; $i < count($target_formats); $i++)
        {
            for ($j = 0; $j < count($avail_formats); $j++)
            {
                if ($target_formats[$i] == $avail_formats[$j]['itag'])
                {
                    //echo '<p>Target format found, it is '. $avail_formats[$j]['itag'] .'</p>';
                    $best_format = $j;
                    break 2;
                }
            }
        }

//echo '<p>Out of loop, best_format is '. $best_format .'</p>';
        if ((isset($best_format)) &&
                (isset($avail_formats[$best_format]['url'])) &&
                (isset($avail_formats[$best_format]['type']))
        )
        {
            $redirect_url = $avail_formats[$best_format]['url'];
            $content_type = $avail_formats[$best_format]['type'];
        }
        if (isset($redirect_url))
        {
            header("Location: $redirect_url");
        }
    } // end of else for type not being Download
    ?>
